# coding=utf-8
import sqlite3
import os
from optparse import OptionParser
import random

from sqliteauthenticator import SQLiteAuthenticator
import traceback

parser = OptionParser()
parser.add_option("-f", "--file", dest="filename",
                  help="write report to FILE")
(options, args) = parser.parse_args()

filename = options.filename
if filename == "" or filename == None:
    filename = "users.txt"

authenticator = SQLiteAuthenticator()
#authenticator.login_as_admin()


def randomNum(N):
    random_number = random.sample('0123456789', N)
    return ''.join(random_number)

os.environ["JUPYTERHUB_SQLITEDB_PATH"]="/home/deng/jupyterhub/jupyterhub-localsqliteauthenticator/jupyterhub-users.db"


with open(filename, "r") as f:
    try:
        for user in f.readlines():
            username = user.strip()
            password = randomNum(6)
            print("add user: %s 密码%s" % (username,password))
            authenticator.sqlite_add_user(username=username, password=password)  # password

    except:
        traceback.print_exc()
